import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn,BeforeInsert } from 'typeorm'
import { Exclude,Expose } from 'class-transformer'
import NodeAuth from 'node-auth0'
// import * as jwt from 'jsonwebtoken'
@Entity({
  name: 'user'
})

export class UserEntity {

  @Exclude()
  private nodeAuth:NodeAuth

  constructor(){
    this.nodeAuth = new NodeAuth()
  }

  @PrimaryGeneratedColumn({
    type: 'int',
    name: 'id',
    comment: '主键id'
  })
  id: number

  @Column({
    type: 'varchar',
    nullable: true,
    unique: false,
    name: 'username',
    length: 64,
    comment: '用户姓名'
  })
  username: string

  @Column({
    type: 'int',
    nullable: true,
    unique: false,
    // length: 11,
    name: 'age',
    comment: '年龄'
  })
  age: number

  @Exclude()
  @Column({
    type: 'tinyint',
    default: () => 0,
    name: 'is_del',
    comment: '是否可删除，0-可以，1-不可以'
  })
  isDel: number

  @Exclude()
  @Column({
    type: 'varchar',
    name: 'password',
    comment: '密码'
  })
  password: string

  @CreateDateColumn({
    type: 'timestamp',
    name: 'create_time',
    comment: '创建时间',
    unique: false,
    nullable: true,
  })
  createTime: Date

  @UpdateDateColumn({
    type: 'timestamp',
    name: 'update_time',
    comment: '更新时间',
    unique: false,
    nullable: true,
  })
  updateTime: Date

  @BeforeInsert()
  makePassword():void{
    this.password = this.nodeAuth.makePassword(this.password)
  }

  @Expose()
  isDelStr(): string {
    return this.isDel ? '允许删除' : '不可删除';
  }

  // private get token(){
  //    const {id,username} = this
  //    return jwt.sign({
  //      id,username
  //    },
  //    process.env.SECRET, //加盐
  //    {
  //     expiresIn:'1d'    //过期时间
  //    }
  //    )
  // }

  // public toResponseObject(isShowToken=true){
  //    const {username,nodeAuth,token,password,...params} = this
  //    const responseData = {
  //      username,
  //      ...params
  //    }
  //    if(isShowToken){
  //      return Object.assign(responseData,{token})
  //    }else{
  //      return responseData
  //    }
  // }
}